package org.zhouhe.web.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.zhouhe.web.tag.function.BtnAddFunction;
import org.zhouhe.web.tag.function.BtnEditFunction;

public class EditDialog extends TagSupport {
	private static final long serialVersionUID = 1L;
	private int width;
	private int height;
	private String dialogId;
	private String pageId;
	private String addButton;
	private String addTitle;
	private String editButton;
	private String editTitle;
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			SimplePage page = (SimplePage) this.getParent();
			if(page != null){
				this.pageId = page.getId();
			}else{
				out.println("PageDialogEdit 未找到父标签！");
				return SKIP_PAGE;
			}

			dialogId = pageId+"_edit_dialog";
			
			if(addButton != null){
				String eventName = "add_"+ page.getId() +"_grid_record";
				page.addButton("<a href='javascript:void(0)' class='easyui-linkbutton' iconCls='icon-add' plain='true' onclick='"+ eventName +"()'>"+ addButton +"</a>");
				page.addFunction(new BtnAddFunction(eventName, page.getId()+"_edit_dialog", addTitle, null).toString());
			}
			if(editButton != null){
				String parentId = page.getId();
				String eventName = "edit_"+ parentId +"_grid_record";
				page.addButton("<a href='javascript:void(0)' class='easyui-linkbutton' iconCls='icon-edit' plain='true' onclick='"+ eventName +"()'>"+ editButton +"</a>");
				page.addFunction(new BtnEditFunction(eventName, page.getId()+"_edit_dialog", parentId+"_grid", editTitle, null).toString());
			}
			
			String dialogButtonsId = dialogId +"_buttons";
	
			//写入当前标签：
			out.print("\r\n<div id='"+ dialogId +"' class='easyui-dialog' style='");
			if(width > 0){
				out.print("width:"+ width +"px;");
			}
			if(height > 0){
				out.print("height:"+ height +"px;");
			}
			out.print("' data-options=\"buttons: '#"+ dialogButtonsId +"',closed:true,modal:true,fit:true\">\r\n");
			//写入保存按钮：
			out.println();
			out.println("	<div id='"+ dialogButtonsId +"'>");
			out.println("		<a href='javascript:void(0)' class='easyui-linkbutton' onclick=\"javascript:alert('保存"+ dialogId +"的数据。')\" data-options=\"iconCls:'icon-ok'\">保存</a>");
			out.println("		<a href='javascript:void(0)' class='easyui-linkbutton' onclick=\"javascript:$('#"+ dialogId +"').dialog('close')\">关闭</a>");
			out.println("	</div>");
			out.println("	<form action='#' onsubmit='return false;'>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			out.println("    </form>");
			out.println("</div>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
	
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}

	public String getAddButton() {
		return addButton;
	}

	public void setAddButton(String addButton) {
		this.addButton = addButton;
	}

	public String getAddTitle() {
		return addTitle;
	}

	public void setAddTitle(String addTitle) {
		this.addTitle = addTitle;
	}

	public String getEditButton() {
		return editButton;
	}

	public void setEditButton(String editButton) {
		this.editButton = editButton;
	}

	public String getEditTitle() {
		return editTitle;
	}

	public void setEditTitle(String editTitle) {
		this.editTitle = editTitle;
	}

}
